 var mongoose = require('mongoose'); //引用mongoose模块
 var dbconfig = require('../config/default')
 var Schema = mongoose.Schema;
 var db = mongoose.connection;
 mongoose.connect(dbconfig.mongodb); //数据库连接
 db.on('error', console.error.bind(console, '连接错误:'));

 var moment = require('moment');
 var objectIdToTimestamp = require('objectid-to-timestamp');
 var marked = require('marked')
     //注册用户
 var UserSchema = new mongoose.Schema({
     name: { type: 'String' },
     password: { type: 'String' },
     avatar: { type: 'String' },
     gender: { type: 'String', enum: ['m', 'f', 'x'] },
     bio: { type: 'String' }
 }, { versionKey: false });
 UserSchema.virtual('createAt').get(function() {
     return moment(objectIdToTimestamp(this._id)).format('YYYY-MM-DD HH:mm')
 });
 //文章
 var PostSchema = new mongoose.Schema({
     author: { type: Schema.Types.ObjectId },
     title: { type: 'string' },
     content: { type: 'string' },
     pv: { type: 'number' },
     tags: { type: 'array' }
 }, { versionKey: false });
 UserSchema.virtual('html').get(function() {
     return marked(this.connect)
 });
 //评论
 var CommentSchema = mongoose.Schema({
     author: { type: Schema.Types.ObjectId },
     content: { type: 'string' },
     postId: { type: Schema.Types.ObjectId }
 }, { versionKey: false });
 //标签
 var TagSchema = new mongoose.Schema({
     author: { type: Schema.Types.ObjectId },
     name: { type: 'string' },
     tagId: { type: Schema.Types.ObjectId },
     status: { type: 'number' }
 }, { versionKey: false })


 //创建model
 module.exports.UserModel = db.model('User', UserSchema);
 module.exports.PostModel = db.model('Post', PostSchema);
 module.exports.CommentModel = db.model('Comment', CommentSchema);
 module.exports.TagModel = db.model('Tag', TagSchema);